package com.capgemini.daos;

import java.math.BigInteger;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.capgemini.data.Athlete;
import com.capgemini.data.Performance;
import com.capgemini.data.Tournament;

public class TournamentDao extends Dao<Tournament>{

	public void delete(Tournament type) {
		EntityManager em = EntityManagerFactoryHolder.get()
				.createEntityManager();

		EntityTransaction tx = em.getTransaction();

		try {
			tx.begin();
			type=em.find(Tournament.class, type.getId());
			for(Performance temp1 : type.getPerformances()){
				type.getPerformances().remove(temp1);
			}
			type=em.merge(type);
			Query q = em.createNativeQuery("delete from performance where tournament_fk= :tournament_fk");
			q.setParameter("tournament_fk", BigInteger.valueOf(type.getId()));
			q.executeUpdate();
			em.remove(type);
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}

			em.close();
		}
	}

}
